package com.qf.agro.web.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.qf.agro.common.sys.ResultInfo;
import com.qf.agro.pojo.sys.SysModule;
import com.qf.agro.service.sys.SysModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/module")
public class SysModelController {

    @Autowired
    private SysModuleService sysModuleService;

    @GetMapping("/page")
    public ResultInfo findPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               String name) {
        //1.调用业务层查询
        Page<SysModule> page = sysModuleService.findPage(pageNum, pageSize, name);
        //2.返回
        return ResultInfo.success(page);
    }

    @GetMapping("/list")
    public ResultInfo list() {
        List<SysModule> list = sysModuleService.list();
        return ResultInfo.success(list);
    }

    @GetMapping("/roleModule")
    public ResultInfo roleModule(Long roleId) {
        //1.调用业务层查询
        List<Long> moduleIds = sysModuleService.findByRoleId(roleId);
        //2.返回
        return ResultInfo.success(moduleIds);
    }

    @PostMapping("/roleModule")
    public ResultInfo updateUserModule(@RequestBody Map<String, Object> map) {
        Long roleId = Long.parseLong((String) map.get("roleId"));
        List<Long> moduleIds = (List<Long>) map.get("moduleIds");
        sysModuleService.updateUserModule(roleId, moduleIds);
        return ResultInfo.success(null);
    }

    /**
     * 动态加载菜单
     * @return
     */
    @GetMapping("/menu")
    public ResultInfo findMenu(){
        //1.调用业务层查询
        List<SysModule> moduleList = sysModuleService.findUserMenu();
        //2.返回
        return ResultInfo.success(moduleList);
    }
}
